package com.younion.action.manage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.younion.business.IMsgTemplateService;
import com.younion.common.base.BaseAction;
import com.younion.common.constants.Status;
import com.younion.common.constants.Template;
import com.younion.common.constants.TemplateScope;
import com.younion.common.constants.TemplateTargetType;
import com.younion.common.constants.TemplateType;
import com.younion.common.paginator.Paginator;
import com.younion.vo.MsgTemplateVO;

import cpcns.jhsks.util.MapCollectionUtils;

/**
 * @author Joe Chen
*/
public class MsgTemplateAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory.getLog(MsgTemplateAction.class);

	private IMsgTemplateService msgTemplateService = null;

	private MsgTemplateVO msgTemplateVO = new MsgTemplateVO();

	private List<MsgTemplateVO> msgTemplateList; 
	
	private List<Status> statuses = Arrays.asList(Status.values());
	private List<TemplateScope> scopes = Arrays.asList(TemplateScope.values());
	private List<TemplateType> types = Arrays.asList(TemplateType.values());
	private List<Template> templates = Arrays.asList(Template.values());
	private List<TemplateTargetType> templateTargets = Arrays.asList(TemplateTargetType.values());
	
	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	@RequiresPermissions("listMsgTemplate")
	public String listMsgTemplate() {
		try {
			Map paramMap = null;
			if (msgTemplateVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(msgTemplateVO));
				if(getSessionUser().getCustomer() != null){
					paramMap.put("customerId", getSessionUser().getCustomer().getId());
				}
			}
			// 获取总记录数
			int total = msgTemplateService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			msgTemplateList = msgTemplateService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	@RequiresPermissions("addMsgTemplate")
	public String addMsgTemplate() {
		return SUCCESS;
	}

	/**
	 *修改保存，调度Jsp
	 */
	@RequiresPermissions("editMsgTemplate")
	public String editMsgTemplate() {
		try {
			msgTemplateVO = msgTemplateService.getById(msgTemplateVO.getId().toString());
			msgTemplateVO.setCustomerNames(msgTemplateService.generateCustomerNames(msgTemplateVO.getCustomerIds()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 保存动作
	 */
	@RequiresPermissions("saveMsgTemplate")
	public String saveMsgTemplate() {
		try {
			if(getSessionUser().getCustomer() != null && getSessionUser().getCustomer().getId() != null){
				msgTemplateVO.setType(TemplateType.SPECIFIC.getName());
				msgTemplateVO.setCustomerIds(getSessionUser().getCustomer().getId().toString());
			}
			msgTemplateVO = msgTemplateService.save(msgTemplateVO);
			String id = msgTemplateVO.getId() == null ? "" : msgTemplateVO.getId().toString();
			String log = "".equals(id) ? "添加模板" : "更新模板";
			Integer type = "".equals(id) ? 3 : 4;
			this.addLog(type, log+id, log,getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "msgtemplateList");
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("deleteMsgTemplate")
	public String deleteMsgTemplate() {
		try {
			String pkId = request.getParameter("ids");
			msgTemplateService.remove(Arrays.asList(pkId.split(",")));
			this.addLog(3, "删除模板："+pkId, "删除模板",getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "msgtemplateList");
		return SUCCESS;
	}
	/**
	 * 查看鿴
	 */
	@RequiresPermissions("viewMsgTemplate")
	public String viewMsgTemplate() {
		try {
			msgTemplateVO = msgTemplateService.getById(msgTemplateVO.getId().toString());
			msgTemplateVO.setCustomerNames(msgTemplateService.generateCustomerNames(msgTemplateVO.getCustomerIds()));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}
	
	@RequiresPermissions("approveMsgTemplate")
	public String approveMsgTemplate() {
		try {
			String pkId = request.getParameter("ids");
			msgTemplateService.updateStatus(Arrays.asList(pkId.split(",")), Status.ACTIVE.getName());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "msgtemplateList");
		return SUCCESS;
	}
	@RequiresPermissions("unactiveMsgTemplate")
	public String unactiveMsgTemplate() {
		try {
			String pkId = request.getParameter("ids");
			msgTemplateService.updateStatus(Arrays.asList(pkId.split(",")), Status.UNACTIVE.getName());
			this.addLog(3, "禁用模板："+pkId, "禁用模板",getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "msgtemplateList");
		return SUCCESS;
	}
	@RequiresPermissions("activeMsgTemplate")
	public String activeMsgTemplate() {
		try {
			String pkId = request.getParameter("ids");
			msgTemplateService.updateStatus(Arrays.asList(pkId.split(",")), Status.ACTIVE.getName());
			this.addLog(3, "启用模板："+pkId, "启用模板",getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "msgtemplateList");
		return SUCCESS;
	}
	
	public MsgTemplateVO getMsgTemplateVO() {
		return msgTemplateVO;
	}

	public void setMsgTemplateVO(MsgTemplateVO msgTemplateVO) {
		this.msgTemplateVO = msgTemplateVO;
	}

	public void setMsgTemplateService(IMsgTemplateService msgTemplateService) {
		this.msgTemplateService = msgTemplateService;
	}

	public List<MsgTemplateVO> getMsgTemplateList() {
		return msgTemplateList;
	}

	public void setMsgTemplateList(List<MsgTemplateVO> msgTemplateList) {
		this.msgTemplateList = msgTemplateList;
	}

	public List<Status> getStatuses() {
		return statuses;
	}

	public void setStatuses(List<Status> statuses) {
		this.statuses = statuses;
	}

	public List<TemplateScope> getScopes() {
		return scopes;
	}

	public void setScopes(List<TemplateScope> scopes) {
		this.scopes = scopes;
	}

	public List<TemplateType> getTypes() {
		return types;
	}

	public void setTypes(List<TemplateType> types) {
		this.types = types;
	}

	
	public List<Template> getTemplates() {
		return templates;
	}

	public void setTemplates(List<Template> templates) {
		this.templates = templates;
	}

	public List<TemplateTargetType> getTemplateTargets() {
		return templateTargets;
	}

	public void setTemplateTargets(List<TemplateTargetType> templateTargets) {
		this.templateTargets = templateTargets;
	}

	public IMsgTemplateService getMsgTemplateService() {
		return msgTemplateService;
	}
	
}